<?php
namespace Backend\Model;
use Think\Model;

class CodeGroupModel extends Model
{
    // 设置添加时表单中允许接收的字段【安全】
    protected $insertFields = 'id,media_id,group_name,update_time';
    // 设置修改的表单中允许出现的字段
    protected $updateFields = 'id,media_id,group_name,update_time';
    // 定义表单验证的规则
    protected $_validate = array(
        array('media_id', 'require', '必须要选择媒体！', 1),
        array('group_name', 'require', '分组名称不能为空！', 1),
        array('group_name', '', '分组名称已存在！', 0, 'unique', 3),
    );


    public function get_code_group_list($pageSize=10){
        $where = array('user_id' =>$_SESSION['id']);
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        $data['data'] = $this->alias('a')
            ->field('a.id,a.group_name,a.update_time,b.name')
            ->where($where)
            ->join('left join dsp_media as b on a.media_id =b.id')
            ->order('id desc')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
        return $data;
    }


    // 添加前
    protected function _before_insert(&$data)
    {
        $data['update_time']=time();
        $data['user_id'] =$_SESSION['id'];
    }
    // 修改前
    protected function _before_update(&$data)
    {
        $data['update_time']=time();
    }

}